package com.soda.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.soda.bean.Menu;
import com.soda.dao.MenuDao;
import com.soda.dao.impl.MenuDaoImpl;
import com.soda.service.MenuService;
import com.soda.util.MenuInfo;
import com.soda.util.MyBatisUtil;
import com.soda.util.PageInfo;
import com.soda.util.SelectRoleInfo;

public class MenuServiceImpl implements MenuService{

	MenuDao md=new MenuDaoImpl();
	//查询所有子菜单
	public List<MenuInfo> getAllMenu(SelectRoleInfo smi, PageInfo pi) {
		// TODO Auto-generated method stub
		SqlSession session=MyBatisUtil.getSession();
		pi.setStartIndex();
		Map<String,Object> map=new HashMap<String,Object>();
		if(smi!=null){
			map.put("name", smi.getName());
			map.put("state", smi.getState());
		}
		if(pi!=null){
			map.put("startIndex", pi.getStartIndex());
			map.put("per", pi.getPer());
		}
		List<MenuInfo> menuInfos=session.getMapper(MenuDao.class).getAllMenu(map);
		MyBatisUtil.closeSession(session);
		return menuInfos;
	}
	/*
	 * 根据查询信息，查询所有符合条件的菜单条数
	 * @see com.soda.service.MenuService#getMenuCountByInfo(com.soda.util.SelectRoleInfo)
	 */
	public int getMenuCountByInfo(SelectRoleInfo smi) {
		SqlSession session=MyBatisUtil.getSession();
		int count=session.getMapper(MenuDao.class).getMenuCountByInfo(smi);
		MyBatisUtil.closeSession(session);
		return count;
	}
	/*
	 *通过id查找菜单
	 * @see com.soda.service.MenuService#getMenuById(int)
	 */
	public MenuInfo getMenuById(int id) {
		// TODO Auto-generated method stub
		SqlSession session=MyBatisUtil.getSession();
		MenuInfo mi=session.getMapper(MenuDao.class).getMenuById(id);
		MyBatisUtil.closeSession(session);
		return mi;
	}
	/*
	 * 更新菜单信息
	 * @see com.soda.service.MenuService#updateMenuInfo(com.soda.bean.Menu)
	 */
	
	public int updateMenuInfo(Menu menu) {
		SqlSession session=MyBatisUtil.getSession();
		int count=session.getMapper(MenuDao.class).updateMenuInfo(menu);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int editState(int id, int state) {
		// TODO Auto-generated method stub
		SqlSession session=MyBatisUtil.getSession();
		int count=session.getMapper(MenuDao.class).editState(id,state);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int deleteMenuById(int id) {
		SqlSession session=MyBatisUtil.getSession();
		int count=session.getMapper(MenuDao.class).deleteMenuById(id);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public void addMenuInfo(Menu menu) {
		SqlSession session=MyBatisUtil.getSession();
		session.getMapper(MenuDao.class).addMenuInfo(menu);
		MyBatisUtil.closeSession(session);
	}
	public int ajaxMenName(String menname) {
		SqlSession session=MyBatisUtil.getSession();
		if(session.getMapper(MenuDao.class).ajaxMenName(menname)>0){
			MyBatisUtil.closeSession(session);
			return 1;
		}
		MyBatisUtil.closeSession(session);
		return 0;
	}
	public String getMenuNameById(int id) {
		SqlSession session=MyBatisUtil.getSession();
		String name=session.getMapper(MenuDao.class).getMenuNameById(id);
		MyBatisUtil.closeSession(session);
		return name;
	}

}
